import Vue from "vue";
import VueI18n from "vue-i18n";
import ElementLocale from "element-ui/lib/locale";
import enLocale from "element-ui/lib/locale/lang/en";
import zhLocale from "element-ui/lib/locale/lang/zh-CN";
import langZh from "./zh";
import langEn from "./en";
Vue.use(VueI18n);
import Cookies from "js-cookie";
var locale = window.localStorage.getItem("lang") || Cookies.get("lang") || "zh";
const i18n = new VueI18n({
  locale,
  messages: {
    zh: { ...langZh, ...zhLocale },
    en: { ...langEn, ...enLocale },
  },
});
ElementLocale.i18n((key, value) => i18n.t(key, value));
export default i18n;

export const translate = function (keys) {
  if (!keys) {
    return "";
  }
  const messages = i18n.messages[i18n.locale];
  if (!messages) {
    return keys;
  }
  let value = null;
  try {
    eval(`value = messages.${keys}`);
  } catch (err) {}
  return value || keys;
};
